package com.yuncheng.spcyApi.flow;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.busService.BusService;
import com.yuncheng.spcyApi.busService.InitWjjlService;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.jcy.JcydjbConstant;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.spcyApi.dto.SjxdSqrBeanDto;
import com.yuncheng.spcyApi.dto.SpcySjxdJbxxDto;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.mapper.SpcyRsHcyMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.spcyService.SqsxYwxxInfoService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.LocalFileUploadUtils;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.flow.AgentVo;
import com.yuncheng.spcyApi.vo.flow.SpcySjxdFqVo;
import com.yuncheng.spcyApi.vo.jcy.HcyJbxxVo;
import com.yuncheng.spcyApi.vo.jcy.RsHcyVo;
import com.yuncheng.spcyApi.vo.spcy.SpcyReturnVo;
import com.yuncheng.spcyApi.vo.spcy.SpcySjxdXpryVo;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.GetKsKz;
import com.yuncheng.vo.HttpResult;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;
import utils.Util;

import javax.annotation.Resource;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hua
 */
@Component(value = "spcySjxdFlowService")
public class SpcySjxdFlowService {

    private static final Logger log = LoggerFactory.getLogger(SpcySjxdFlowService.class);

    @Resource
    @Lazy
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Resource
    @Lazy
    private ISpcySqsxJobService spcySqsxJobService;

    @Resource
    @Lazy
    private ISysFlowNodeService sysFlowNodeService;

    @Resource
    @Lazy
    private FlowNodeService flowNodeService;

    @Resource
    @Lazy
    private IYcSysUserService ycSysUserService;

    @Resource
    @Lazy
    private ISysUserFgldService sysUserFgldService;

    @Resource
    @Lazy
    private BusService busService;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    private ISpcySjxdJbxxService spcySjxdJbxxService;

    @Resource
    private ISpcySjxdXpryService spcySjxdXpryService;

    @Resource
    private ISpcySqsxWjjlService spcySqsxWjjlService;

    @Resource
    private SpcyRsHcyMapper spcyRsHcyMapper;

    @Resource
    private ISpcySqsxXpjlService spcySqsxXpjlService;

    @Resource
    private InitWjjlService initWjjlService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private SqsxYwxxInfoService sqsxYwxxInfoService;

    /**
     * 数据修订方案-从原事项复制一份文件
     * */
    private Result copySqsxFaWjjl(String sqsxid, String fsjxdid){
        List<SpcySqsxWjjl> list = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, sqsxid)
                .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.jcfa));

        for (SpcySqsxWjjl oldWjjl : list){
            SpcySqsxWjjl copyWjjl = new SpcySqsxWjjl();
            BeanUtil.copyProperties(oldWjjl, copyWjjl);
            copyWjjl.setId(GetUuIdUtils.ReplaceUuId());
            copyWjjl.setFpid(fsjxdid);

            String oldWjlx = "";
            String oldCflj = "";

            if (StringUtils.isNotBlank(oldWjjl.getFjdcflj())) {

                oldWjlx = Util.getWjHz(oldWjjl.getFjdcflj());
                oldCflj = oldWjjl.getFjdcflj();

                copyWjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);

            }else {

                oldWjlx = oldWjjl.getFwjlx();
                oldCflj = oldWjjl.getFcflj();

                copyWjjl.setFwjlx4(SpcyCommonConstant.SQSX_WJJL_TYPE4_SDSC);
            }

            String ml = "/" + DateConversionUtils.DateToMonthTwo(new Date());
            String wjccmc = GetUuIdUtils.ReplaceUuId() + oldWjlx;
            String newCflj = ml + "/" + wjccmc;
            busService.initLocalTempFileMl(ml);

            String absUrl = "";
            if (localFileUploadUtils.isEnableMinioWjjlFile()) {
                File file = localFileUploadUtils.convertMinioToFile(localFileUploadUtils.getSqsxWjjlRelativeUrl(oldCflj), oldWjlx);
                if (file != null){
                    absUrl = file.getAbsolutePath();
                }
            }else {
                absUrl = localFileUploadUtils.getSqsxWjjlAbsUrl(oldCflj);
            }

            /** 复制文件 */
            if (busService.uploadMinioFileWjjl(newCflj, absUrl)){
                copyWjjl.setFwjlx(oldWjlx);
                copyWjjl.setFwjccmc(wjccmc);
                copyWjjl.setFcflj(newCflj);
                copyWjjl.setFjdcflj("");
                copyWjjl.setCreateTime(new Date());
                copyWjjl.setUpdateTime(new Date());
                spcySqsxWjjlService.save(copyWjjl);
            }
        }

        return Result.ok();
    }

    /**
     * 查询数据修订方案文件列表
     * */
    public HttpResult selectSjxdFaWjList(String fsjxdid){
        return HttpResult.ok(spcySqsxWjjlService.getWjjlListByPidAndType(fsjxdid, WjmlConstant.jcfa, false));
    }

    /**
     * 同步更新事项基本信息
     */
    private Result syncUpdateOrgSqsxJbxx(String fsjxdid, SpcySjxdJbxx sjxd){
        if (sjxd == null) {
            sjxd = spcySjxdJbxxService.getById(fsjxdid);
        }

        if (sjxd.getFxglx().equals(SpcyCommonConstant.SPCY_SJXD_JBXX_XGLX_JCXX)) {
            // 同步基本信息
            LambdaUpdateWrapper<SpcySqsxBasic> orgSqsxWrapper = new LambdaUpdateWrapper<>();
            orgSqsxWrapper.eq(SpcySqsxBasic::getId, sjxd.getFsqsxid());

            SpcySqsxBasic updateOrgSqsx = new SpcySqsxBasic();
            updateOrgSqsx.setFcykssj(sjxd.getFxdjckssj());
            updateOrgSqsx.setFcyjssj(sjxd.getFxdjcjssj());
            spcySqsxBaiscService.update(updateOrgSqsx, orgSqsxWrapper);

            // 同步选派记录时间
            spcySqsxXpjlService.syncUpdateXpjlGzsjBySqsx(sjxd.getFsqsxid(), updateOrgSqsx.getFcykssj(), updateOrgSqsx.getFcyjssj());

            // 同步选派人员
            this.syncUpdateOrgSqsxXpjlJbxx(fsjxdid, sjxd);

        }else if (sjxd.getFxglx().equals(SpcyCommonConstant.SPCY_SJXD_JBXX_XGLX_FAXG)){

            // 同步文件
            this.syncUpdateOrgSqsxWjInfo(fsjxdid, sjxd);
        }

        return Result.ok();
    }

    /**
     * 同步修改原申请事项文件信息
     * */
    private Result syncUpdateOrgSqsxWjInfo(String fsjxdid, SpcySjxdJbxx sjxd){
        if (sjxd == null) {
            sjxd = spcySjxdJbxxService.getById(fsjxdid);
        }

        List<SpcySqsxWjjl> sjxdWjList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, fsjxdid)
                .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.jcfa));

        boolean isDelOrgWjFlag = true;
        List<SpcySqsxWjjl> orgWjList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, sjxd.getFsqsxid())
                .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.jcfa));

        /** 将数据修订的方案文件替换原事项的方案文件 */
        for (SpcySqsxWjjl oldWjjl : sjxdWjList){
            SpcySqsxWjjl copyWjjl = new SpcySqsxWjjl();
            BeanUtil.copyProperties(oldWjjl, copyWjjl);
            copyWjjl.setId(GetUuIdUtils.ReplaceUuId());
            copyWjjl.setFpid(sjxd.getFsqsxid());

            String oldWjlx = oldWjjl.getFwjlx();
            String oldCflj = oldWjjl.getFcflj();

            String ml = "/" + DateConversionUtils.DateToMonthTwo(new Date());
            String wjccmc = GetUuIdUtils.ReplaceUuId() + oldWjlx;
            String newCflj = ml + "/" + wjccmc;

            String newJdCflj = "";
            if (StringUtils.isNotBlank(oldWjjl.getFjdcflj())){
                newJdCflj = ml + "/" + GetUuIdUtils.ReplaceUuId() + Util.getWjHz(oldWjjl.getFjdcflj());
            }

            busService.initLocalTempFileMl(ml);

            String cfAbsUrl = "";
            String jdCfAbsUrl = "";

            if (localFileUploadUtils.isEnableMinioWjjlFile()) {
                File cfFile = localFileUploadUtils.convertMinioToFile(localFileUploadUtils.getSqsxWjjlRelativeUrl(oldCflj), oldWjlx);
                if (cfFile != null){
                    cfAbsUrl = cfFile.getAbsolutePath();
                }

                if (StringUtils.isNotBlank(newJdCflj)) {
                    File jdfile = localFileUploadUtils.convertMinioToFile(localFileUploadUtils.getSqsxWjjlRelativeUrl(oldWjjl.getFjdcflj()), Util.getWjHz(oldWjjl.getFjdcflj()));
                    if (jdfile != null) {
                        jdCfAbsUrl = jdfile.getAbsolutePath();
                    }
                }

            }else {
                cfAbsUrl = localFileUploadUtils.getSqsxWjjlAbsUrl(oldCflj);

                if (StringUtils.isNotBlank(newJdCflj)) {
                    jdCfAbsUrl = localFileUploadUtils.getSqsxWjjlAbsUrl(oldWjjl.getFjdcflj());
                }
            }

            /** 复制文件 */
            if (busService.uploadMinioFileWjjl(newCflj, cfAbsUrl)){
                copyWjjl.setFwjlx(oldWjlx);
                copyWjjl.setFwjccmc(wjccmc);
                copyWjjl.setFcflj(newCflj);

                if (busService.uploadMinioFileWjjl(newJdCflj, jdCfAbsUrl)) {
                    copyWjjl.setFjdcflj(newJdCflj);
                }
                copyWjjl.setCreateTime(new Date());
                copyWjjl.setUpdateTime(new Date());
                boolean save = spcySqsxWjjlService.save(copyWjjl);
                if (save){
                    /** 删除原文件 */
                    if (isDelOrgWjFlag) {
                        boolean delFlag = initWjjlService.deleteCommonSqsxFile(orgWjList);
                        if (delFlag){
                            isDelOrgWjFlag = false;
                        }
                    }
                }
            }
        }
        return Result.ok();
    }

    /**
     * 同步事项-选派人员
     */
    private Result syncUpdateOrgSqsxXpjlJbxx(String fsjxdid, SpcySjxdJbxx sjxd){
        if (sjxd == null) {
            sjxd = spcySjxdJbxxService.getById(fsjxdid);
        }

        List<SpcySjxdXpry> xdXpryList = spcySjxdXpryService.list(new LambdaQueryWrapper<SpcySjxdXpry>()
                .eq(SpcySjxdXpry::getFsjxdid, fsjxdid));
        if (CollectionUtil.isEmpty(xdXpryList)){
            return Result.ok();
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxInfoById(sjxd.getFsqsxid());

        List<String> addUserIdList = new ArrayList<>(xdXpryList.size()); // 添加用户id
        List<String> removeUserIdList = new ArrayList<>(xdXpryList.size()); // 移除用户id
        boolean isExistsAddYbjcy = false; // 是否存在新增预备检查员
        List<String> removeYbjcyIdList = new ArrayList<>(xdXpryList.size()); // 移除预备检查员
        List<String> addZzUserIdList = new ArrayList<>(xdXpryList.size()); // 添加组长用户id
        List<String> removeZzUserIdList = new ArrayList<>(xdXpryList.size()); // 移除组长用户id

        for (SpcySjxdXpry xdXpry : xdXpryList){
            if (xdXpry.getFxdlx().equals(SpcyCommonConstant.SPCY_SJXD_XPRY_XDLX_REMOVE)) {

                List<SpcySqsxXpjl> orgSqsxXpList = spcySqsxXpjlService.list(new LambdaQueryWrapper<SpcySqsxXpjl>()
                        .eq(SpcySqsxXpjl::getFsqsxid, sjxd.getFsqsxid())
                        .eq(SpcySqsxXpjl::getFuserid, xdXpry.getFuserid()));

                if (CollectionUtil.isNotEmpty(orgSqsxXpList)){
                    String operContent = "";
                    operContent += "数据修订-删除选派人员：";
                    operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(orgSqsxXpList.get(0)));
                    spcyLogService.addLogs(LogConstant.LOGS_SQSX_XPJL, operContent, sjxd.getFsqsxid(), "数据修订-删除选派人员", SpcyConstant.LOGS_SYS_TYPE_SC);

                    SpcySqsxXpjl xpjl = orgSqsxXpList.get(0);
                    xpjl.setFsftyxp(SpcyConstant.XPJL_SFTYXP_NO);
                    if (StringUtils.isNotBlank(xpjl.getFbz())) {
                        xpjl.setFbz(xpjl.getFbz() + "/数据修订移除人员");
                    }else {
                        xpjl.setFbz("数据修订移除人员");
                    }
                    spcySqsxXpjlService.updateById(xpjl);
                }

                removeUserIdList.add(xdXpry.getFuserid());

                if (xdXpry.getFjcsf().equals(SpcyConstant.HCY_SSK_ZZ)){
                    removeZzUserIdList.add(xdXpry.getFuserid());

                } else if (xdXpry.getFjcsf().equals(SpcyConstant.HCY_SSK_YB)){
                    removeYbjcyIdList.add(xdXpry.getFuserid());
                }
            }

            if (xdXpry.getFxdlx().equals(SpcyCommonConstant.SPCY_SJXD_XPRY_XDLX_ADD)) {

                RsHcyVo rsHcyVo = new RsHcyVo();
                /** 待补充检查员id */
                rsHcyVo.setId(xdXpry.getFjcyid());
                List<RsHcyVo> rsHcyList = spcyRsHcyMapper.getRsHcyList(rsHcyVo);

                try {
                    Result result = busService.initSjxdXpHcy(sqsx,
                            rsHcyList,
                            sjxd.getFsqsxid(),
                            SpcyConstant.HCY_USERTYPE_JCY,
                            DateConversionUtils.DateToStringYYYYMMDD(sjxd.getFxdjckssj()),
                            DateConversionUtils.DateToStringYYYYMMDD(sjxd.getFxdjcjssj()),
                            sjxd.getFsqrid(), sjxd.getFsqrmc(), "数据修订新增人员",
                            xdXpry.getFjcsf());
                    if (!result.isSuccess()){
                        log.error("1同步数据修订新增选派人员错误：事项id{}, 修订id{}", sjxd.getFsqsxid(), fsjxdid);
                        log.error("1同步数据修订新增选派人员错误：{}", result.getMessage());
                    }

                    if (CollectionUtil.isNotEmpty(rsHcyList)) {
                        addUserIdList.add(xdXpry.getFuserid());

                        if (xdXpry.getFjcsf().equals(SpcyConstant.HCY_SSK_ZZ)){
                            addZzUserIdList.add(xdXpry.getFuserid());
                        }else if (xdXpry.getFjcsf().equals(SpcyConstant.HCY_SSK_YB)){
                            isExistsAddYbjcy = true;
                        }
                    }

                }catch (Exception e){
                    log.error("2同步数据修订新增选派人员错误：事项id{}, 修订id{}", sjxd.getFsqsxid(), fsjxdid);
                    log.error("2同步数据修订新增选派人员错误：{}", e.getMessage());
                }
            }
        }

        // 同步修改相关文件内容
        initWjjlService.syncJcyInfoFileBySjxd(sjxd.getFsqsxid());

        // 数据修订-同步更新选派人员相关数据
        sqsxYwxxInfoService.syncJcyInfoDataWithSjxd(
                sjxd.getFsqsxid(), sjxd, sqsx,
                isExistsAddYbjcy, removeYbjcyIdList,
                addZzUserIdList, removeZzUserIdList,
                addUserIdList, removeUserIdList);

        return Result.ok();
    }

    /**
     * 初始化-数据修订基本信息
     * */
    public HttpResult initSjxdData(SpcySjxdFqVo initBean){
        if (StringUtils.isBlank(initBean.getSqsxid())){
            return HttpResult.error("事项参数为空！");
        }
        if (StringUtils.isBlank(initBean.getFxglx())){
            return HttpResult.error("请选择修改类型！");
        }

        List<SpcySjxdJbxx> sjList = spcySjxdJbxxService.selectSjxdByFlcztAndFsqsxid(SpcyFlowConstant.LCZT_LCZB, initBean.getSqsxid());
        if (CollectionUtil.isNotEmpty(sjList)){
            return HttpResult.error("该事项当前已存在修订流程，请先完成再进行申请！");
        }

        SpcySjxdJbxx newSdxd = new SpcySjxdJbxx();
        BeanUtil.copyProperties(initBean, newSdxd);
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(initBean.getSqsxid());
        if (sqsx == null){
            return HttpResult.error("没有相匹配的事项数据！");
        }

        newSdxd.setId(GetUuIdUtils.ReplaceUuId());
        newSdxd.setFsqsxid(initBean.getSqsxid());

        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        newSdxd.setFsqrid(currentUser.getId());
        newSdxd.setFsqrmc(currentUser.getName());
        newSdxd.setFsqbm(currentUser.getOrgName());

        if (newSdxd.getFsqrq() == null){
            newSdxd.setFsqrq(new Date());
        }

        newSdxd.setFsxmc(sqsx.getFsxmc());
        newSdxd.setFqymc(sqsx.getFqymc());
        newSdxd.setFslbh(sqsx.getFslbh());
        newSdxd.setFywflcode(sqsx.getFywflcode());

        newSdxd.setFyjckssj(sqsx.getFcykssj());
        newSdxd.setFyjcjssj(sqsx.getFcyjssj());

        newSdxd.setFxdjckssj(newSdxd.getFyjckssj());
        newSdxd.setFxdjcjssj(newSdxd.getFyjcjssj());
        newSdxd.setFlczt(SpcyFlowConstant.LCZT_LCZB);

        spcySjxdJbxxService.save(newSdxd);
        SpcySqsxJob job = null;

        Result result = this.initJob(newSdxd.getId(), newSdxd.getFsqrid(), currentUser);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }else {
            job = (SpcySqsxJob) result.getResult();
            if (initBean.getFxglx().equals(SpcyCommonConstant.SPCY_SJXD_JBXX_XGLX_FAXG)) {
                Result result1 = this.copySqsxFaWjjl(newSdxd.getFsqsxid(), newSdxd.getId());
                if (!result1.isSuccess()) {
                    log.error("初始化-数据修订基本信息-错误：{}", result1.isSuccess());
                }
            }
        }

        Map resultMap = this.getJobConcernByJobId(job.getId(), job, newSdxd);

        return HttpResult.ok(resultMap);
    }

    /**
     * 更新数据修订信息
     * */
    public Result updateSjxdData(HashMap<String,Object> map, String id){
        if (map == null || map.isEmpty()){
            return Result.ok();
        }

        if (StringUtils.isBlank(id)) {
            try {
                id = map.get("id").toString();
            }catch (Exception e){
                return Result.error("参数丢失为空！");
            }

        }
        if (StringUtils.isBlank(id)){
            return Result.error("参数为空！");
        }

        SpcySjxdJbxx updateBean = spcySjxdJbxxService.getById(id);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(updateBean, map);
        } catch (IllegalAccessException e) {
            log.error("更新现场检查章节出错：【IllegalAccessException】-> {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新现场检查章节出错：【InvocationTargetException】-> {}",e.getMessage());
        }

        spcySjxdJbxxService.updateById(updateBean);

        return Result.ok();
    }

    /**
     * 删除数据修订信息
     */
    public HttpResult deleteSjxdData(String jobid){
        if (StringUtils.isBlank(jobid)){
            return HttpResult.error("流程参数为空！");
        }

        SpcySqsxJob job = spcySqsxJobService.getById(jobid);
        if (job == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        Result result = flowNodeService.bjCommonJob(jobid, job, "撤销申请");
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }
        spcySjxdJbxxService.updateFlcztById(job.getFpid(), SpcyFlowConstant.LCZT_LCCX);

        return HttpResult.ok("撤销成功");
    }

    /**
     * 新增修订人员
     */
    public HttpResult addSjxdXpryData(SpcySjxdXpryVo initBean){
        if (StringUtils.isBlank(initBean.getXzid())){
            return HttpResult.error("人员参数为空！");
        }
        if (StringUtils.isBlank(initBean.getId())){
            return HttpResult.error("修订参数为空！");
        }
        if (StringUtils.isBlank(initBean.getFxdlx())){
            return HttpResult.error("修订类型为空！");
        }

        SpcySjxdXpry sjxdXpry = new SpcySjxdXpry();

        if (initBean.getFxdlx().equals(SpcyCommonConstant.SPCY_SJXD_XPRY_XDLX_ADD)){
            RsHcyVo jcy = spcyRsHcyMapper.selectJcyJbxxById(initBean.getXzid());
            /** 待补充记录检查原id */
            sjxdXpry.setFjcsf(jcy.getFssk());
            sjxdXpry.setFjcyid(initBean.getXzid());
            sjxdXpry.setFuserid(jcy.getFuserid());
            sjxdXpry.setFjcyxm(jcy.getRealname());

            String sqsxid = spcySjxdJbxxService.selectXdSqsxIdById(initBean.getId());
            List<String> list = spcySqsxXpjlService.selectFsftyxpIdByFsqsxidAndFuserid(sqsxid, sjxdXpry.getFuserid(), SpcyConstant.XPJL_SFTYXP_YES);
            if (CollectionUtil.isNotEmpty(list)){
                return HttpResult.error(sjxdXpry.getFjcyxm()+"在该事项已经被【同意】选派过了！无法再次操作【新增】修订该人员！");
            }

        }else if (initBean.getFxdlx().equals(SpcyCommonConstant.SPCY_SJXD_XPRY_XDLX_REMOVE)){
            SpcySqsxXpjl xpjl = spcySqsxXpjlService.getById(initBean.getXzid());
            sjxdXpry.setFjcsf(xpjl.getFssk());
            sjxdXpry.setFuserid(xpjl.getFuserid());
            sjxdXpry.setFjcyxm(xpjl.getFjcyxm());
            sjxdXpry.setFjcyid(xpjl.getFjcyid());
        }

        if (StringUtils.isNotBlank(initBean.getFjcsf())){
            sjxdXpry.setFjcsf(initBean.getFjcsf());
        }

        List<SpcySjxdXpry> oldXdryList = spcySjxdXpryService.list(new LambdaQueryWrapper<SpcySjxdXpry>()
                .eq(SpcySjxdXpry::getFsjxdid, initBean.getId())
                .eq(SpcySjxdXpry::getFuserid, sjxdXpry.getFuserid()));
        if (CollectionUtil.isNotEmpty(oldXdryList)){
            return HttpResult.error("当前修订人员已经选择过该人员了！");
        }

        sjxdXpry.setId(GetUuIdUtils.ReplaceUuId());
        sjxdXpry.setFsjxdid(initBean.getId());
        sjxdXpry.setFxdlx(initBean.getFxdlx());
        spcySjxdXpryService.save(sjxdXpry);

        return HttpResult.ok();
    }

    /**
     * 删除修订人员
     */
    public HttpResult deleteSjxdXpryData(String id){

        spcySjxdJbxxService.removeById(id);

        return HttpResult.ok("删除成功");
    }

    /**
     * 查询新增修订人员-列表
     */
    public HttpResult selectAddSjxdUserList(RsHcyVo query, String fsjxdid, PageVo pageVo){
        if (StringUtils.isBlank(fsjxdid)){
            return HttpResult.error("修订参数为空！");
        }

        SpcySjxdJbxxDto sjxd = spcySjxdJbxxService.selectCommonJbxxById(fsjxdid);

        Page<HcyJbxxVo> page = new Page<>(pageVo.getPageNo(),pageVo.getPageSize());

        query.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
        query.setFywflcode(spcySqsxBaiscService.getYwflCodeBySqsxId(sjxd.getFsqsxid()));

        if (sjxd.equals(SpcyCommonConstant.SPCY_SJXD_JBXX_XGLX_JCXX)){
            query.setKssj(sjxd.getFxdjckssj());
            query.setJssj(sjxd.getFxdjcjssj());
        }else {
            query.setKssj(sjxd.getFyjckssj());
            query.setJssj(sjxd.getFyjcjssj());
        }

        /** 在这个检查范围有任务的人员,标识是否执行任务 */
        query.setCxkssj(DateConversionUtils.DateToStringYYYYMMDD(query.getKssj()));
        query.setCxjssj(DateConversionUtils.DateToStringYYYYMMDD(query.getJssj()));

        query.setFjcyxl(JcydjbConstant.getJcyXlByYwflcode(query.getFywflcode()));
        query.setFywflcode(null);
        query.setFlx(null);
        query.setFusertype(null);

        if (StringUtils.isNotBlank(query.getFgpk()) && query.getFgpk().equals(SpcyConstant.JCY_GKP_YES)){
            query.setFgpk(SpcyConstant.JCY_GKP_YES);
        }else {
            query.setFgpk(null);
        }

        query.setId(null);
        IPage<HcyJbxxVo> pageList = spcyRsHcyMapper.pageJcyXpList(page, query);
        for (HcyJbxxVo bean : pageList.getRecords()) {
            if (bean.getBxpsl() > 0) {
                bean.setFgzzt(SpcyConstant.JCY_ZY_ZXRWZ);
            }else {
                bean.setFgzzt(SpcyConstant.JCY_ZT_ZG);
            }
        }

        return HttpResult.ok(pageList);
    }

    /**
     * 查询移除修订人员-列表
     */
    public HttpResult selectRemoveSjxdUserList(String fsjxdid){
        if (StringUtils.isBlank(fsjxdid)){
            return HttpResult.error("修订参数为空！");
        }

        String sqsxid = spcySjxdJbxxService.selectXdSqsxIdById(fsjxdid);
        return HttpResultVo.HttpResultList(spcySqsxXpjlService.getTyxpUserBeanListByFsqsxId(sqsxid));
    }

    /**
     * 查询数据修订人员
     */
    public HttpResult selectSjxdRyList(String fsjxdid){
        if (StringUtils.isBlank(fsjxdid)){
            return HttpResult.error("修订参数为空！");
        }

        Map resultMap = new HashMap();

        SpcySjxdJbxxDto sjxd = spcySjxdJbxxService.selectCommonJbxxById(fsjxdid);
        resultMap.put("sjxd", sjxd);

        List<SpcySjxdXpry> xdryList = spcySjxdXpryService.list(new LambdaQueryWrapper<SpcySjxdXpry>()
                .eq(SpcySjxdXpry::getFsjxdid, fsjxdid)
                .orderByAsc(SpcySjxdXpry::getCreateTime));

        resultMap.put("addList", xdryList.stream()
                .filter( f->f.getFxdlx().equals(SpcyCommonConstant.SPCY_SJXD_XPRY_XDLX_ADD)).collect(Collectors.toList()));

        resultMap.put("removeList", xdryList.stream()
                .filter( f->f.getFxdlx().equals(SpcyCommonConstant.SPCY_SJXD_XPRY_XDLX_REMOVE)).collect(Collectors.toList()));

        resultMap.put("orgList", spcySqsxXpjlService.getTyxpUserBeanListByFsqsxId(sjxd.getFsqsxid()));

        return HttpResult.ok(resultMap);
    }

    /**
     * 验证数据修订人员-移除对应身份，是否新增对应的身份人员
     * */
    private Result verifyRemoveEqAdd(String fsjxdid){
        List<SpcySjxdXpry> xdryList = spcySjxdXpryService.list(new LambdaQueryWrapper<SpcySjxdXpry>()
                .eq(SpcySjxdXpry::getFsjxdid, fsjxdid));
        if (CollectionUtil.isEmpty(xdryList)){
            return Result.ok();
        }

        // 验证是否存在移除组长
        List<SpcySjxdXpry> removeZzList = xdryList.stream()
                .filter(f -> f.getFxdlx().equals(SpcyCommonConstant.SPCY_SJXD_XPRY_XDLX_REMOVE)
                        && f.getFjcsf().equals(SpcyConstant.HCY_SSK_ZZ)
                )
                .collect(Collectors.toList());
        int removeZzSize = removeZzList.size();
        if (removeZzSize == 0){
            return Result.ok();
        }

        /** 验证移除组长数量与
         * 原组长数量是否一致，
         * 或者 小于原组长人数 ，
         * 不满足返回提示
         * */
        String sqsxid = spcySjxdJbxxService.selectXdSqsxIdById(fsjxdid);
        Integer orgTyZzSize = spcySqsxXpjlService.selectTySskSize(sqsxid, SpcyConstant.XPJL_SFTYXP_YES, SpcyConstant.HCY_SSK_ZZ);
        if (orgTyZzSize == removeZzSize){

            List<SpcySjxdXpry> addZzXdryList = xdryList.stream()
                    .filter(f -> f.getFxdlx().equals(SpcyCommonConstant.SPCY_SJXD_XPRY_XDLX_ADD)
                            && f.getFjcsf().equals(SpcyConstant.HCY_SSK_ZZ)
                    )
                    .collect(Collectors.toList());

            if (CollectionUtil.isEmpty(addZzXdryList)){
                return Result.error("操作失败，组长人数不足，请至少选择一名组长！");
            }

        }

        return Result.ok();
    }

    /**
     * 数据修订-查询流程任务详情
     * */
    public Map getJobConcernByJobId(String jobid, SpcySqsxJob job, SpcySjxdJbxx sjxd){
        Map map = new HashMap();

        if (job == null) {
            job = spcySqsxJobService.getById(jobid);
        }

        if (sjxd == null) {
            sjxd = spcySjxdJbxxService.getById(job.getFpid());
        }

        map.put("sjxd", sjxd); // 当前流转记录
        map.put("job", job); // 当前流转记录

        try {
            map.putAll(this.getNextNodeDbr(jobid, job));
        }catch (Exception e){
            log.error("数据修订-查询流程任务详情-错误：{}", e.getMessage());
        }


        return map;
    }

    /**
     * 数据修订-查询流程历史记录
     * */
    public HttpResult queryJobHistoryList(String fsjxdid){

        return HttpResultVo.HttpResultList(spcySqsxJobService.selectHistoryListByFpidAndFlcmc(fsjxdid, null));
    }

    /**
     * 处理流程
     * @param flowVo 流程处理参数
     * @param jobId 流程任务id
     * @param pid 流程业务id
     * @param submitType 0: 保存；1: 提交
     * @param opinion 办理意见
     * @return
     */
    public Result handleFlow(FlowVo flowVo, String jobId, String pid, String submitType, String opinion){
        return this.handleFlow(flowVo, jobId, null, pid, submitType, opinion);
    }

    /**
     * 处理流程
     * @param nowJob 当前流程任务
     * @return
     */
    public Result handleFlow(FlowVo flowVo, String jobId, SpcySqsxJob nowJob, String pid, String submitType, String opinion){
        if (StringUtils.isBlank(jobId)){
            return Result.error("流程任务id参数丢失！");
        }

        if (nowJob == null) {
            // 当前流程任务
            nowJob = spcySqsxJobService.getById(jobId);

            if (nowJob == null){
                return Result.error("当前流程任务不存在，请重试!");
            }
        }

        pid = nowJob.getFpid();
        if (StringUtils.isBlank(pid)){
            return Result.error("业务参数为空！");
        }

        // 提交按钮
        if (submitType.equals(SpcyFlowConstant.SUBMIT_TYPE_SUBMIT)) {
            if (nowJob == null) {
                return Result.error("当前流程任务不存在");
            }
        }

        if (StringUtils.isBlank(nowJob.getFhjmc())){
            return Result.error("流程环节名称为空！");
        }

        if (nowJob.getFhjmc().equals(SpcyFlowConstant.HJMC_FQSJXD)){

            if (submitType.equals(SpcyFlowConstant.SUBMIT_TYPE_SUBMIT)){
                // 验证是否存在 移除组长 对应新增 组长
                Result result = this.verifyRemoveEqAdd(pid);
                if (!result.isSuccess()){
                    return result;
                }
            }

            // 流程环节处理
            Result httpResult = handleNodeName(flowVo, submitType, nowJob, pid, opinion);
            if (!httpResult.isSuccess()){
                return httpResult;
            }

        }
        else if (nowJob.getFhjmc().equals(SpcyFlowConstant.SPCY_SJXD_HJMC_END)){

            // 流程环节处理
            Result httpResult = handleNodeName(flowVo, submitType, nowJob, pid, opinion);
            if (!httpResult.isSuccess()){
                return httpResult;
            }

            if (submitType.equals(SpcyFlowConstant.SUBMIT_TYPE_SUBMIT)){

                initWjjlService.verifyAutoFileGzByCommon(pid, WjmlConstant.jcfa);

                // 流程状态提交
                spcySjxdJbxxService.updateFlcztById(pid, SpcyFlowConstant.LCZT_LCBJ);

                // 同步数据修订信息到原事项上
                Result result = this.syncUpdateOrgSqsxJbxx(pid, null);
                if (!result.isSuccess()){
                    log.error("同步数据修订信息到原事项上-错误: 修订id:{}; msg: {}", pid, result.getMessage());
                }

            }

        }else {

            return Result.error(nowJob.getFhjmc() + "该环节不属于当前流程提交！");
        }

        return Result.ok("操作成功");
    }

    /**
     * 流程环节处理
     * @param pid 业务id
     * @param flowVo 需要修改的字段内容
     * @param job 流程任务job
     * @param opinion 办理意见
     * @param submitType 提交、保存类型
     * @return true(),false(提示信息)
     */
    private Result handleNodeName(FlowVo flowVo,String submitType, SpcySqsxJob job, String pid, String opinion){
        HashMap<String, Object> map = flowVo.getMap();
        String smgflag = flowVo.getSmgflag(); // 短信发送标志
        String smsContent = flowVo.getSmsContent(); // 发送短信内容

        Result res = null;

        // 保存按钮
        if (submitType.equals(SpcyFlowConstant.SUBMIT_TYPE_SAVE)){
            res = this.updateSjxdData(map, pid);
            if (!res.isSuccess()){
                return res;
            }else {
                return Result.ok("保存成功");
            }
        }

        // 提交按钮
        if (submitType.equals(SpcyFlowConstant.SUBMIT_TYPE_SUBMIT)){
            res = this.updateSjxdData(map, pid);
            if (!res.isSuccess()){
                return res;
            }

            // 流程任务id
            String jobid = job.getId();

            // 提交流程任务,待办人是否为空，指定待办人
            if (StringUtils.isBlank(flowVo.getDbrid())) {
                // 按审查模式-提交-流程任务
                res = this.passJob(pid, jobid, job, opinion, flowVo.getNextNode(),flowVo.getNextNodeId(), null);
            }else {
                // 按审查模式-提交-流程任务
                res = this.passJob(pid, jobid, job, opinion, flowVo.getNextNode(),flowVo.getNextNodeId(),flowVo.getDbrid());
            }

            if (!res.isSuccess()){
                return res;
            }else {
                /**
                 * 接收返回的流程任务，如果返回的流程任务id不相等，发送短信
                 * 发送下环节短信
                 */
                if (StringUtils.isNotBlank(smgflag) && (smgflag.equals("是") || smgflag.equals("true"))){
                    busService.sendSmsToNextJob(jobid, smsContent);
                }
            }
        }

        return Result.ok("提交成功");
    }
    /**
     * 退回流程
     */
    public HttpResult getReturnUpperFlowJob(SpcyReturnVo spcyReturnVo){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("登录已失效，请重新登录！");
        }

        if (StringUtils.isBlank(spcyReturnVo.getJobid())){
            return HttpResult.error("流程任务参数为空！");
        }

        if (StringUtils.isBlank(spcyReturnVo.getOpinion())){
            return HttpResult.error("请填写退回意见！");
        }

        SpcySqsxJob currentJob = spcySqsxJobService.getById(spcyReturnVo.getJobid());
        if (currentJob == null){
            return HttpResult.error("退回失败！当前流程任务不存在！");
        }

        SpcySqsxJob previousJob = getCurrentJobPrevious(spcyReturnVo.getJobid(), currentJob);
        if (previousJob == null){
            return HttpResult.error("退回失败！选中的退回环节不存在！");
        }

        // 退回功能
        Result result = flowNodeService.returnJob(spcyReturnVo.getJobid(), currentJob, spcyReturnVo.getShjid(), previousJob, spcyReturnVo.getOpinion(), currentUser);
        if (result.isSuccess()){
            if (StringUtils.isNotBlank(spcyReturnVo.getSmgflag()) && (spcyReturnVo.getSmgflag().equals("是") || spcyReturnVo.getSmgflag().equals("true"))) {
                SpcySqsxJob newPreviousJob = (SpcySqsxJob) result.getResult();
                // 发送退回短信
                String nextNodeSmsContent = busService.getNextNodeSmsContent(newPreviousJob, null);
                busService.sendNextNodeSmsContent(newPreviousJob, nextNodeSmsContent);
            }
        }

        return HttpResult.ok("退回成功");
    }

    /**
     * 查询审评查验流程当前环节-上一环节
     * @param jobid 流程任务id
     * @return
     */
    private SpcySqsxJob getCurrentJobPrevious(String jobid, SpcySqsxJob nowJob){
        SpcySqsxJob previousJob = null;

        if (nowJob == null) {
            nowJob = spcySqsxJobService.getById(jobid);
            if (nowJob == null) {
                throw new BusinessException("当前流程任务不存在！");
            }
        }

        if (StringUtils.isBlank(nowJob.getFnodeid())){
            throw new BusinessException("当前流程任务结点配置不存在！");
        }

        List<SysFlowNode> previousNodeList = sysFlowNodeService.getPreviousNode(nowJob.getFnodeid());
        if (CollectionUtil.isEmpty(previousNodeList)){
            return previousJob;
        }

        LambdaQueryWrapper<SpcySqsxJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxJob::getFpid, nowJob.getFpid());
        queryWrapper.orderByAsc(SpcySqsxJob::getFfssj);

        if (previousNodeList.size() == 1) {
            queryWrapper.eq(SpcySqsxJob::getFnodeid, previousNodeList.get(0).getId());
        }else {
            List<String> preIdsList = previousNodeList.stream().map(SysFlowNode::getId).collect(Collectors.toList());
            queryWrapper.in(SpcySqsxJob::getFnodeid, preIdsList);
        }

        // 上环节结点id-办理过的流程任务
        List<SpcySqsxJob> previousJobList = spcySqsxJobService.list(queryWrapper);

        for (int i=0; i<previousJobList.size(); i++){
            previousJob = previousJobList.get(0);
        }

        return previousJob;
    }

    /**
     * 初始化流程
     * @param pid 业务id
     * @param currentUser 当前登陆用户
     * @return
     */
    private Result initJob(String pid, CurrentUserVo currentUser){
        return initJob(pid, "", currentUser);
    }
    private Result initJob(String pid, String dbrId, CurrentUserVo currentUser){

        //获取流程模板的第一个环节
        SysFlowTemple code = sysFlowNodeService.getCode(SpcyFlowConstant.SPCY_SJXD_FLOW_CODE);

        List<SysFlowNode> node = sysFlowNodeService.getFirstNode(code.getId());

        LambdaQueryWrapper<SpcySqsxJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxJob::getFpid, pid);
        queryWrapper.eq(SpcySqsxJob::getFnodeid, node.get(0).getId());
        queryWrapper.ne(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB);

        List<SpcySqsxJob> jobList = spcySqsxJobService.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(jobList)){
            return Result.error("当前环节["+node.get(0).getFjdmc()+"】已经存在待办，请办理完再进行操作!");
        }

        Result result = flowNodeService.createJob(SpcyFlowConstant.SPCY_SJXD_FLOW_CODE, pid, dbrId, node.get(0),null,null,currentUser);
        if (!result.isSuccess()){
            return result;
        }

        return result;
    }

    /**
     * 提交流程
     * @param pid 业务id
     * @param jobid 流程任务id
     * @param job 当前流程任务
     * @param opinion 办理意见
     * @param nextNodeName 下环节结点名称
     * @param nextNodeId 下环节结点id
     * @param nextNodeDbrIds 下环节待办人
     * @return
     */
    private Result passJob(String pid,String jobid,SpcySqsxJob job, String opinion,String nextNodeName,String nextNodeId,String nextNodeDbrIds){
        if (job == null){
            job = spcySqsxJobService.getById(jobid);
        }
        String jobNodeName = job.getFhjmc(); // 当前环节名称

        Result httpResult = null;

        // 是否为最终结点
        if (StringUtils.isNotBlank(jobNodeName) && jobNodeName.equals(SpcyFlowConstant.SPCY_SJXD_HJMC_END)){
            //办理掉当前环节
            httpResult = flowNodeService.doJob(job, opinion);
        }else {
            httpResult = flowNodeService.passJob(SpcyFlowConstant.SPCY_SJXD_FLOW_CODE, pid, jobid, opinion, nextNodeId, nextNodeDbrIds);
        }

        if (!httpResult.isSuccess()){
            return httpResult;
        }

        return Result.ok();
    }

    /**
     * 获取下环节待办人
     * @param jobid
     * @param job
     * @return
     */
    public Map getNextNodeDbr(String jobid, SpcySqsxJob job){
        Map map = new HashMap();

        Map nextDbr = this.getNowJobInNextDbrAndHandlePageUrl(jobid, job);
        String nextNodeId = ""; // 下结点id
        String nextNode = "办结"; // 下结点名称
        if (nextDbr.get("nextNode") != null) {
            SysFlowNode nextFlowNode = (SysFlowNode) nextDbr.get("nextNode");
            nextNodeId = nextFlowNode.getId();
            nextNode = nextFlowNode.getFjdmc();
        }
        List<AgentVo> dbrListByMap = this.getDbrListByMap(nextDbr);

        map.put("nextNodeDbrList", dbrListByMap); // 下环节待办人list

        map.put("nextNode", nextNode); // 下流程结点名称
        map.put("nextNodeId", nextNodeId); // 下流程结点id

        return map;
    }

    /**
     * 获取下一个环节名称和待办人
     * @param job 流程任务
     * @param jobid 流程任务id
     * @return
     * {
     *     dbrId
     *     dbr:
     *     nextNode：下流程结点
     *     handlePageUrl：办理页面路径
     * }
     */
    private Map getNowJobInNextDbrAndHandlePageUrl(String jobid, SpcySqsxJob job){
        if (job == null && StringUtils.isNotBlank(jobid)){
            job = spcySqsxJobService.getById(jobid);
        }

        return flowNodeService.getNowJobInNextDbrAndHandlePageUrl(SpcyFlowConstant.SPCY_SJXD_FLOW_CODE, job, null);
    }

    /**
     * 将待办人map转为List
     * @param map { dbrId: 待办人id ; dbr: 待办人 }
     */
    private List<AgentVo> getDbrListByMap(Map map){
        String dbrids = map.get("dbrId").toString(); // 待办人id
        String dbrs = map.get("dbr").toString(); // 待办人

        List<AgentVo> agentList = new ArrayList<>();

        if (StringUtils.isNotBlank(dbrids)) {
            String agentIds[] = dbrids.split(",|，");
            String agentNames[] = dbrs.split(",|，");

            for (int i = 0; i < agentIds.length; i++) {
                AgentVo agentVo = new AgentVo();
                agentVo.setDbrid(agentIds[i]);
                agentVo.setDbr(agentNames[i]);
                agentList.add(agentVo);
            }
        }

        return agentList;
    }

    /**
     * 获取当前环节待办人
     * @param pid 业务id
     * @param nodeName 环节名称
     * @return
     */
    public Map getNodeDbr(String pid, String nodeName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";

        /** 获取对应科室 */
        SjxdSqrBeanDto sjxdJbxx = spcySjxdJbxxService.selectSjxdSqrBeanById(pid);
        String deptName = "";
        if (sjxdJbxx != null){
            deptName = sjxdJbxx.getFsqbm();
        }

        // 发起
        if (nodeName.equals(SpcyFlowConstant.HJMC_FQSJXD)){
            if (sjxdJbxx != null) {
                dbrId = sjxdJbxx.getFsqrid();
                dbr = sjxdJbxx.getFsqrmc();
            }
        }

        // 审核
        if (nodeName.equals(SpcyFlowConstant.HJMC_SJXDSH)){
            // 科室负责人
            Map kzMap = getKsfzrByDeptName(deptName);
            dbrId = kzMap.get("dbrId").toString();
            dbr = kzMap.get("dbr").toString();
        }

        // 审批
        /*if (nodeName.equals(SpcyFlowConstant.HJMC_SJXDSP)){
            // 分管领导
            Map kzMap = getKsFgldByDeptName(deptName);
            dbrId = kzMap.get("dbrId").toString();
            dbr = kzMap.get("dbr").toString();
        }*/

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 对应科室的 科室负责人
    private Map getKsfzrByDeptName(String deptName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(deptName, SpcyConstant.GW_NAME_KZ);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId()+",";
                dbr += ksKz.getRealname()+",";
            }
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 获取对应科室的 分管领导
    private Map getKsFgldByDeptName(String deptName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";

        YcSysUser fgldUserBydept = sysUserFgldService.getFgldUserBydept(deptName);
        if (fgldUserBydept != null) {
            dbrId = fgldUserBydept.getId();
            dbr = fgldUserBydept.getRealname();
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }
}
